﻿using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class UIFlowTex : MonoBehaviour
{
    public static string ShaderDefaultPath = "Unlit/UIFlowTex";

    public Texture EffectTex;
    public Vector2 EffectScale = new Vector2(1, 1);
    public Color EffectColor = Color.gray;
    public bool IsUseImageColor = false;
    [Range(0, 4f)]
    public float Brightness = 1;
    public Vector2 Speed;
    public Texture MaskTex;
    public Texture NoiseTex;
    [Range(0.01f, 3f)]
    public float NoiseAdjust = 1;
    public Texture DistortTex;
    [Range(0, 1)]
    public float DistortStrength;
    public Vector2 DistortScale = new Vector2(1, 1);
    public Vector2 DistortSpeed;
    //[Range(0, 1f)]
    //public float EffStrength = 1;
    public bool IsOnlyEffTex = true;
    public bool IsAdditive;
    public bool IsAlphaTex;

    private Material FlowTexMat;

    void Start()
    {
        Shader FlowLightShader;
        FlowLightShader = Shader.Find(ShaderDefaultPath);

        Image image = GetComponent<Image>();
        Texture mainTex = image.mainTexture;

        if (FlowLightShader != null && FlowLightShader.isSupported)
        {
            FlowTexMat = new Material(FlowLightShader);
            image.material = FlowTexMat;

            FlowTexMat.SetFloat("_DstBlend", IsAdditive ? 1 : 10);

            //处理图集,精度调整
            Vector4 uvRect = UnityEngine.Sprites.DataUtility.GetOuterUV(image.overrideSprite);
            uvRect.x = float.Parse(uvRect.x.ToString("f2"));
            uvRect.y = float.Parse(uvRect.y.ToString("f2"));
            uvRect.z = float.Parse(uvRect.z.ToString("f2"));
            uvRect.w = float.Parse(uvRect.w.ToString("f2"));
            //Debug.Log("uvRect: x = " + uvRect.x + " y = "+ uvRect.y + " z = " + uvRect.z + " w = " + uvRect.w);
            FlowTexMat.SetVector("_UvRect", uvRect);

            FlowTexMat.SetTexture("_MainTex", mainTex);
            FlowTexMat.SetTexture("_EffectTex", EffectTex);
            FlowTexMat.SetVector("_EffectScale", EffectScale);
            FlowTexMat.SetTexture("_Mask", MaskTex);
            FlowTexMat.SetTexture("_Noise", NoiseTex);
            FlowTexMat.SetTexture("_DistortTex", DistortTex);
            FlowTexMat.SetFloat("_IsOnlyEffTex", IsOnlyEffTex? 1 : 0);
            FlowTexMat.SetFloat("_IsAlphaTex", IsAlphaTex ? 1 : 0);

            FlowTexMat.SetVector("_Speed", Speed);
            FlowTexMat.SetColor("_EffectColor", EffectColor);
            FlowTexMat.SetFloat("_UseVertexColor", IsUseImageColor ? 1 : 0);
            FlowTexMat.SetFloat("_Brightness", Brightness);
            FlowTexMat.SetFloat("_NoiseAdjust", NoiseAdjust);
            FlowTexMat.SetVector("_DistortParams", new Vector4(DistortScale.x, DistortScale.y, DistortSpeed.x, DistortSpeed.y));
            FlowTexMat.SetFloat("_DistortStrength", DistortStrength);
        }
    }

    void Update()
    {
#if UNITY_EDITOR
        if (FlowTexMat)
        {
            FlowTexMat.SetVector("_Speed", Speed); 
            FlowTexMat.SetColor("_EffectColor", EffectColor);
            FlowTexMat.SetFloat("_Brightness", Brightness);
            FlowTexMat.SetFloat("_NoiseAdjust", NoiseAdjust);
            FlowTexMat.SetVector("_DistortParams", new Vector4(DistortScale.x, DistortScale.y, DistortSpeed.x, DistortSpeed.y));
            FlowTexMat.SetFloat("_DistortStrength", DistortStrength);
        }
#endif
    }
}
